package com.molyfun.parents.im.chatrow;

import android.app.Activity;
import android.content.Context;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.util.FileUtils;
import com.hyphenate.util.TextFormater;
import com.molyfun.parents.R;
import com.molyfun.parents.im.model.ChatMessageEntity;
import com.molyfun.parents.im.model.NormalFileMessageBody;

import java.io.File;

public class ChatRowFile extends BaseChatRow{

    protected TextView fileNameView;
	protected TextView fileSizeView;
    protected TextView fileStateView;

    protected EMCallBack sendfileCallBack;

    protected boolean isNotifyProcessed;
    private NormalFileMessageBody fileMessageBody;

    public ChatRowFile(Context context, ChatMessageEntity message, int position, BaseAdapter adapter) {
		super(context, message, position, adapter);
	}

	@Override
	protected void onInflateView() {
	    inflater.inflate(message.direct() == EMMessage.Direct.RECEIVE ? 
	            R.layout.ease_row_received_file : R.layout.ease_row_sent_file, this);
	}

	@Override
	protected void onFindViewById() {
	    fileNameView = (TextView) findViewById(R.id.tv_file_name);
        fileSizeView = (TextView) findViewById(R.id.tv_file_size);
        fileStateView = (TextView) findViewById(R.id.tv_file_state);
        percentageView = (TextView) findViewById(R.id.percentage);
	}


	@Override
	protected void onSetUpView() {
	    fileMessageBody = (NormalFileMessageBody) message.getBody();
        String filePath = fileMessageBody.getLocalUrl();
        fileNameView.setText(fileMessageBody.getFileName());
        fileSizeView.setText(TextFormater.getDataSize(fileMessageBody.getFileSize()));
        if (message.direct() == EMMessage.Direct.RECEIVE) {
            File file = new File(filePath);
            if (file.exists()) {
                fileStateView.setText(R.string.Have_downloaded);
            } else {
                fileStateView.setText(R.string.Did_not_download);
            }
            return;
        }

        // until here, to sending message
//        handleSendMessage();
	}

//	/**
//	 * handle sending message
//	 */
//    protected void handleSendMessage() {
////        setMessageSendCallback();
//        switch (message.status()) {
//        case SUCCESS:
//            if(progressBar != null)
//            progressBar.setVisibility(View.INVISIBLE);
//            if(percentageView != null)
//                percentageView.setVisibility(View.INVISIBLE);
//            if(statusView != null)
//            statusView.setVisibility(View.INVISIBLE);
//            break;
//        case FAIL:
//            if(progressBar != null)
//            progressBar.setVisibility(View.INVISIBLE);
//            if(percentageView != null)
//                percentageView.setVisibility(View.INVISIBLE);
//            if(statusView != null)
//            statusView.setVisibility(View.VISIBLE);
//            break;
//        case INPROGRESS:
//            if(progressBar != null)
//            progressBar.setVisibility(View.VISIBLE);
//            if(percentageView != null){
//                percentageView.setVisibility(View.VISIBLE);
//                //TODO 下载进度
////                percentageView.setText(message.progress() + "%");
//            }
//            if(statusView != null)
//            statusView.setVisibility(View.INVISIBLE);
//            break;
//        default:
//            if(progressBar != null)
//            progressBar.setVisibility(View.INVISIBLE);
//            if(percentageView != null)
//                percentageView.setVisibility(View.INVISIBLE);
//            if(statusView != null)
//            statusView.setVisibility(View.VISIBLE);
//            break;
//        }
//    }
//

	@Override
    protected void onUpdateView() {
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onBubbleClick() {
        String filePath = fileMessageBody.getLocalUrl();
        File file = new File(filePath);
        if (file.exists()) {
            // open files if it exist
            FileUtils.openFile(file, (Activity) context);
        } else {
            // TODO download the file
//            context.startActivity(new Intent(context, EaseShowNormalFileActivity.class).putExtra("msg", message));
        }
    }
}
